package ClasesInterfaces;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.HeadlessException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.UIManager;

import java.awt.Font;

import javax.swing.SwingConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;

import javax.swing.JPasswordField;

import ClasesControladoras.ControladorUsuario;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class InterfazUsuario extends JFrame {

	private JPanel contentPane;
	private JTextField textUsuario;
	
	String usuario;
	String clave;
	private JPasswordField passClave;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					InterfazUsuario frame = new InterfazUsuario();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public InterfazUsuario() {
		setResizable(false);
		setTitle("Iniciar Sesi\u00F3n");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		try{
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		}
		catch(Exception e) {  
		}
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblUsuario = new JLabel("Usuario");
		lblUsuario.setBounds(105, 100, 46, 14);
		contentPane.add(lblUsuario);
		
		JLabel lblContrasea = new JLabel("Contrase\u00F1a");
		lblContrasea.setBounds(105, 125, 75, 14);
		contentPane.add(lblContrasea);
		
		textUsuario = new JTextField();
		textUsuario.setBounds(190, 97, 117, 20);
		contentPane.add(textUsuario);
		textUsuario.setColumns(10);
		
		passClave = new JPasswordField();
		passClave.setBounds(190, 122, 117, 20);
		contentPane.add(passClave);
		
		JButton btnIniciarSesion = new JButton("Iniciar Sesi\u00F3n");

		btnIniciarSesion.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				usuario = textUsuario.getText().toLowerCase();
				clave = String.valueOf(passClave.getPassword());
				try {
					if(ControladorUsuario.validarUsuario(usuario,clave)){
						InterfazMenuPrincipal m = new InterfazMenuPrincipal();
						m.setVisible(true);
						dispose();
					}
				} catch (HeadlessException | SQLException e) {
					e.printStackTrace();	
				}
				}}); 
		btnIniciarSesion.setBounds(190, 228, 117, 23);
		contentPane.add(btnIniciarSesion);
		
		JButton btnSalir = new JButton("Salir");
		btnSalir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(EXIT_ON_CLOSE);
			}
		});
		
		btnSalir.setBounds(307, 228, 117, 23);
		contentPane.add(btnSalir);
		
		JLabel lblIniciarSesin = new JLabel("Iniciar Sesi\u00F3n");
		lblIniciarSesin.setHorizontalAlignment(SwingConstants.CENTER);
		lblIniciarSesin.setFont(new Font("Calibri", Font.BOLD, 18));
		lblIniciarSesin.setBounds(105, 27, 222, 30);
		contentPane.add(lblIniciarSesin);
	}
}
